package com.softa.web.test.consumer;

import com.alicp.jetcache.Cache;
import com.alicp.jetcache.anno.CreateCache;
import com.corundumstudio.socketio.SocketIOServer;
import com.softa.common.constant.CacheConstants;
import com.softa.common.dto.MessageSiteDTO;
import com.softa.common.sysconfig.properties.MessageSiteProperties;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * Created by yangl on 2018/9/5.
 */

@Service
@RocketMQMessageListener(topic = "${panda.rocketmq.topic}", consumerGroup = "${panda.rocketmq.consumer}")
public class MessageConsumer implements RocketMQListener<MessageSiteDTO> {

    private Logger logger = LoggerFactory.getLogger(MessageConsumer.class);

    @Autowired
    private SocketIOServer socketIOServer;

    @Autowired
    private MessageSiteProperties messageSiteProperties;

    @CreateCache(name = CacheConstants.MESSAGE_SITE_CACHE_NAME, expire = CacheConstants.TIME_OUT_1800)
    private Cache messageSiteCache;

    @Override
    public void onMessage(MessageSiteDTO siteDTO) {
        logger.info("我接收到消息："+siteDTO.getContent());
        String sessionId = (String) messageSiteCache.get(siteDTO.getUserId());
        if(null != sessionId){
            socketIOServer.getClient(UUID.fromString(sessionId)).sendEvent(messageSiteProperties.getChanel(), siteDTO);
        }
    }
}
